package cn.gson.crm.controller.pay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.gson.crm.controller.BaseServlet;
import cn.gson.crm.model.dao.StudentDao;
import cn.gson.crm.model.dao.StudentPayDao;
import cn.gson.crm.model.dao.StudentPoDao;
import cn.gson.crm.model.entity.Employee;
import cn.gson.crm.model.po.FollowStudentPo;
import cn.gson.crm.model.po.StudentPayPo;

@WebServlet("/student/contract")
public class StudentContractServlet extends BaseServlet {
	private StudentDao stuDao = new StudentDao();

	/**
	 * 学生签约
	 */
	private static final long serialVersionUID = 1L;
	private StudentPoDao stuPoDao = new StudentPoDao();
	private StudentPayDao stuPayDao = new StudentPayDao();

	@Override
	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String p = request.getParameter("page");
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		if (id != null) {
			FollowStudentPo po = stuPoDao.getStudentInfo(id);
			Employee emp = (Employee) request.getSession().getAttribute("emplo");
			request.setAttribute("po", po);
			request.setAttribute("emp", emp);
			request.setAttribute("now", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
			getServletPathAndForward(request, "pay/getcontract.jsp");
			return;
		}
		if (p == null || p.isEmpty()) {
			p = "1";
		}
		Integer page = Integer.parseInt(p);
		List<FollowStudentPo> list = stuPoDao.getNormalStudentList("正常", name,"已分配");
		List<FollowStudentPo> studentdetail = stuPoDao.getListThenDividPage(request, list, page);
		request.setAttribute("page", page);
		request.setAttribute("queryString", noPageQueryString(request));
		request.setAttribute("studentdetailList", studentdetail);
		getServletPathAndForward(request, "pay/contract.jsp");
	}

	@Override
	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String stuId = request.getParameter("stuId");
		String empId = request.getParameter("empId");
		StudentPayPo po = new StudentPayPo();
		po.setId(Integer.parseInt(stuId));
		po.setEmpId(Integer.parseInt(empId));
		po.setIs_allpay(1);
		if (stuPayDao.getContract(po)) {
			stuDao.changeStuGrade("5", stuId,0,0);
			stuDao.changePayStatus(po);
			redirect("student/haventpay");
			return;
		}else{
			request.setAttribute("erro", "签约失败！请重试！");
			forward("pay/getcontract.jsp");
			return;
		}
	}
}
